package com.buaa.airticketbookingsystem.Controller;

import com.buaa.airticketbookingsystem.Entity.User;
import com.buaa.airticketbookingsystem.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
public class RegisterController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Map<String, Object> registerNewUser(@RequestBody Map<String, String> remap){
        Map<String, Object> map = new HashMap<>();
        try{
            User u = userService.selectUserByEmail(remap.get("email"));
            if(u == null){
                userService.registerNewUser(new User(remap.get("user_name"), remap.get("password"), remap.get("email")));
                map.put("res", true);
                map.put("message", "注册成功！");
            }
            else{
                map.put("res", false);
                map.put("message", "该邮箱已注册，请前往登录");
            }
        }catch(Exception e){
            e.printStackTrace();
            map.put("res", false);
            map.put("message", "用户注册失败！，请刷新界面重新登录，或联系管理员解决");
        }
        return map;
    }
}
